/////////////////////////
// 	DDMenu.cpp	:	v0010
//	Written by 	:	Li	Haijun
//	Compiler	:	Microsoft Visual C++ 4.0
//	v0010		:	Nov.07.1996
/////////////////////////

/////////////////////////////////////////////////////////////////////////////
// CDDMenu class
#include	"stdafx.h"
#include	"stdio.h"
#include	"Assert.h"
#include	"DDCompo.h"

#include	"DDButton.h"
#include	"DDMenu.h"
#include	"interfac.h"

#include	"puthz.h"
////////////////////////////
#include "l_allbmp.h"
extern char	bmpfilename[30];
extern char	listfilename[30];
/////////////////////////////////////


extern class	CDDCheckBoxUnit	*	pAllCheckBoxUnit[MAX_CHECKBOXUNIT];	//to restore all check-box's pointer
extern class	CDDListUnit		*	pAllListUnit[MAX_LISTUNIT]; //to record all list-unit's pointer
extern BOOL	FACE_ShowMainBackGround(int PosX, int PosY, int Width, int Height);
///////////////////////////////////////////////////////
//	to define the middel point of the screen
#define		MIDDLE_X	250		// the middle x coordination of screen
#define		MIDDLE_Y	250		// the middle y coordination of screen
// implement function
CDDMenu::CDDMenu()
{
	m_nFileNameId = NONE;
	m_nID	= 0;
	m_szBitmap.cx = 0L;
	m_szBitmap.cy = 0L;
	m_dwX = 0;	m_dwY = 0;

	m_bText = FALSE ;
	m_dTextSer = NONE ;
	memset(m_sText, 0, sizeof(m_sText)) ;
	m_dTextX = 90 ;
	m_dTextY = 170 ;
	m_nTextColumn = 46 ;
	m_nTextRow = 4 ;

	m_bTitle = FALSE ;
	memset(m_sTitle, 0, sizeof(m_sTitle)) ;
	m_dTitleX = 274 ;
	m_dTitleY = 120 ;
}
CDDMenu::~CDDMenu()
{
	m_nFileNameId = NONE;
	m_nID	= 0;
	m_szBitmap.cx = 0L;
	m_szBitmap.cy = 0L;
	m_dwX = 0;	m_dwY = 0;
}
////////////////////////////////////////////////////////
//	to preload the menu bitmap and set some data member
////////////////////////////////////////////////////////
BOOL CDDMenu::PreLoad(int FilenameId, int posX/*=-1*/, int posY/*=-1*/)
{
	char	fname[20];

	HRESULT			ddrval;
	DDSURFACEDESC	ddsd;

//////////////////////////////////////////////////
	_itoa( FilenameId, fname, 10 );

	class CPicture_imageall picture;

	picture.image_open_compress(bmpfilename);
	picture.image_open_index(listfilename);
	picture.LoadBitmap( &m_BitmapSurface, FilenameId );
	picture.image_close_index();
	picture.image_close_compress();

/////////////////////////////////////////////////////////
	// to set the value of m_FileName
	m_nFileNameId = FilenameId;	//set the member veriable m_FileName
    //
    // get size of surface.
    //
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH;

    ddrval = m_BitmapSurface.GetSurface()->GetSurfaceDesc(&ddsd);
	if(ddrval != DD_OK)
		return FALSE;
	m_szBitmap.cx = ddsd.dwWidth;
	m_szBitmap.cy = ddsd.dwHeight;
	if( posX == -1 ) 
	{
		//to set the value of m_dwX, m_dwY
		m_dwX = MIDDLE_X - m_szBitmap.cx/2;
		m_dwY = MIDDLE_Y - m_szBitmap.cy/2;
	}
	else
	{
		m_dwX = posX;
		m_dwY = posY;
	}

	return TRUE;
}
// to set the show position of the back ground bitmap of this menu
void	CDDMenu::SetPosition(int x, int y)
{
	m_dwX = x;
	m_dwY = y;
}
// to set the size of back ground bitmap of this menu
void	CDDMenu::SetSize(int Width, int Height)
{
	m_szBitmap.cx = Width;
	m_szBitmap.cy = Height;
}
// to set the text of this menu
void	CDDMenu::SetString( LPCTSTR text )
{
	if(text[0] != '\0')
	{
		memset(m_sText, 0, sizeof(m_sText)) ;
		m_bText = TRUE ;
		strcpy(m_sText, text) ;
	}
}
// to set the title of this menu
void	CDDMenu::SetTitle( LPCTSTR title )
{
	if(title[0] != '\0')
	{
		memset(m_sTitle, 0, sizeof(m_sTitle)) ;
		m_bTitle = TRUE ;
		strcpy(m_sTitle, title) ;
	}
}
//////////////////////////////////////////////////////
//	to release the background bitmap and command-unit
//////////////////////////////////////////////////////
void	CDDMenu::Release()
{
	m_nFileNameId = NONE;
	m_szBitmap.cx = 0L;
	m_szBitmap.cy = 0L;
	m_dwX = 0;	m_dwY = 0;

	m_bText = FALSE ;
	// release all button in m_MenuButtonUnit
	m_MenuButtonUnit.Release();
	// release the surface that this menu's background bitmap used
	m_BitmapSurface.Release();

}
////////////////////////////////////////////////////////
//	to blit the string belong to this menu
BOOL	CDDMenu::BlitString()
{
	int					iResult ;
	class	CDDText		text;	// the text surface to blit to the back buffer

	iResult = text.BeginText( m_nTextColumn, m_nTextRow );
	if( ! iResult )
		return FALSE;

	if(m_bTitle == TRUE)
	{
		char	temp[1024] ;
		memset( temp, 0, sizeof(temp) ) ;

		for( unsigned int i=0; i<strlen(m_sTitle); i=i+2 )
		{
			temp[2*i] = m_sTitle[i] ;
			temp[2*i+1] = m_sTitle[i+1] ;
			temp[2*i+2] = ' ' ;
			temp[2*i+3] = ' ' ;
		}
		text.SetTextColor( RGB(255, 255, 120) );
		text.PolyTextOut(DD_GetBackBuffer(), m_dTitleX, m_dTitleY, temp);
		// to blit the attribute prompt word to the prompt area
		text.GetDDSurface()->Erase();
	}
	// to set the color of this text show 
	text.SetTextColor( RGB(192, 192, 192) );
	text.PolyTextOut(DD_GetBackBuffer(), m_dTextX, m_dTextY, m_sText);
	// to blit the attribute prompt word to the prompt area
	text.GetDDSurface()->Erase();
	text.EndText();

	return TRUE ;
}
///////////////////////////////////////////////////////////////
// to blit the background picture and the command unit of this menu 
//					to the specify position
///////////////////////////////////////////////////////////////
BOOL CDDMenu::Blit()
{
	if(m_nFileNameId != NONE)
	{
		RECT	BitmapRect;
		BitmapRect.left = BitmapRect.top = 0L;
		BitmapRect.right = m_szBitmap.cx;
		BitmapRect.bottom = m_szBitmap.cy;

		POINT	ShowPoint;
		ShowPoint.x = m_dwX;
		ShowPoint.y = m_dwY;

		m_BitmapSurface.BltToBack(ShowPoint, &BitmapRect);
	}
	// to blit the button unit to the back buffer
	m_MenuButtonUnit.Blit();
	// to blit the string belong to this menu
	if( m_bText )
		BlitString() ;

	return TRUE;
}
///////////////////////////////////////////////////////////////
// to blit only the background picture at the specify position
///////////////////////////////////////////////////////////////
BOOL CDDMenu::BlitBackGround()
{
	if(m_nFileNameId != NONE)
	{
		RECT	BitmapRect;
		BitmapRect.left = BitmapRect.top = 0L;
		BitmapRect.right = m_szBitmap.cx;
		BitmapRect.bottom = m_szBitmap.cy;

		POINT	ShowPoint;
		ShowPoint.x = m_dwX;
		ShowPoint.y = m_dwY;

		m_BitmapSurface.BltToBack(ShowPoint, &BitmapRect);
	}
	return TRUE;
}
/////////////////////////////////////////////////////////////////////////
//	to updata the background of this menu from back buffer to front buffer
//////////////////////////////////////////////////////////////////////////
void CDDMenu::Update(void)
{
	RECT	ShowRect;

	ShowRect.left = m_dwX;
	ShowRect.top = m_dwY;
	ShowRect.right = ShowRect.left + m_szBitmap.cx;
	ShowRect.bottom = ShowRect.top + m_szBitmap.cy;
//	ShowRect.right = m_szBitmap.cx;
//	ShowRect.bottom = m_szBitmap.cy;

	DDC_UpdateScreen( &ShowRect );
}
///////////////////////////////////////////////////////////////
//	to show the menu at the specify position ( by:m_dwX,m_dwY)
///////////////////////////////////////////////////////////////
void CDDMenu::Show()
{
	// to blit the back-ground picture to the back buffer
	Blit();
	// to update the background area( of this menu )from back buffer to the front buffer
	Update();
}
///////////////////////////////////////////////////////////////
//		this member function only use in main menu
// to restore the main back-ground that been modified by this menu
////////////////////////////////////////////////////////////////
void CDDMenu::RestoreBackGround()
{
	// to update the back-ground partly with the main back-ground bitmap
	FACE_ShowMainBackGround(m_dwX, m_dwY, m_szBitmap.cx, m_szBitmap.cy);
}